{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ch09"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 10.1 从文件中提取数据\n",
    "\n",
    "# 10.1.1 txt\n",
    "with open('pi_digits.txt') as file_object:\n",
    "    contents = file_object.read()\n",
    "    print(contents)\n",
    "\n",
    "### 结尾有个空行，用rstrip()去除\n",
    "\n",
    "with open('pi_digits.txt') as file_object:\n",
    "    contents = file_object.read()\n",
    "    print(contents.rstrip())\n",
    "    #### 我这个用完rstrip还是有空行\n",
    "\n",
    "## 10.1.2 绝对路径\n",
    "with open('D:\\python10.txt') as file_object:\n",
    "    contents = file_object.read()\n",
    "    print(contents.rstrip())\n",
    "\n",
    "## 10.1.3 逐行读取\n",
    "\n",
    "filename='pi_digits.txt'\n",
    "\n",
    "with open(filename) as file_object:\n",
    "    for line in file_object:\n",
    "        print(line)\n",
    "\n",
    "filename='pi_digits.txt'\n",
    "\n",
    "with open(filename) as file_object:\n",
    "    for line in file_object:\n",
    "        print(line.rstrip())\n",
    "#\n",
    "## 10.1.4 创建包含文件各行内容的列表readlines\n",
    "filename='pi_digits.txt'\n",
    "\n",
    "with open(filename) as file_object:\n",
    "    lines = file_object.readlines()\n",
    "\n",
    "for line in lines:\n",
    "    print(line.rstrip())\n",
    "print(lines)\n",
    "\n",
    "## 10.1.5 使用文件的内容，列表又变字符串\n",
    "filename='pi_digits.txt'\n",
    "\n",
    "with open(filename) as file_object:\n",
    "    lines = file_object.readlines()\n",
    "\n",
    "pi_string = ''\n",
    "for line in lines:\n",
    "    pi_string += line.rstrip()\n",
    "\n",
    "print(pi_string)\n",
    "print(len(pi_string))\n",
    "\n",
    "## 10.1.6 pi后一百万位\n",
    "\n",
    "filename='pi_million_digits.txt'\n",
    "\n",
    "with open(filename) as file_object:\n",
    "    lines = file_object.readlines()\n",
    "\n",
    "pi_string = ''\n",
    "for line in lines:\n",
    "    pi_string += line.rstrip()\n",
    "\n",
    "print(pi_string[0:52])\n",
    "print(len(pi_string))\n",
    "\n",
    "## 10.1.7 圆周率中包含你的生日嘛\n",
    "filename='pi_million_digits.txt'\n",
    "\n",
    "with open(filename) as file_object:\n",
    "    lines = file_object.readlines()\n",
    "\n",
    "pi_string = ''\n",
    "for line in lines:\n",
    "    pi_string += line.rstrip()\n",
    "\n",
    "birthday = input(\"Enter your birthday, in the form mmddyy:\")\n",
    "\n",
    "if birthday in pi_string:\n",
    "    print('Your birthday appears in the first million digits of pi!')\n",
    "\n",
    "else:\n",
    "    print('Your bt does not')\n",
    "\n",
    "# 10.2 写入文件\n",
    "\n",
    "## 10.2.1 写入空文件，如果文件不存在，会自动创建\n",
    "\n",
    "filename = 'programing.txt'\n",
    "\n",
    "with open(filename,'w') as file_object:\n",
    "    file_object.write('当我们对视，我们眼里有对方，和色彩明丽的希望。')\n",
    "\n",
    "## 10.2.2 换行\n",
    "\n",
    "filename = 'programming.txt'\n",
    "\n",
    "with open(filename,'w') as file_object:\n",
    "    file_object.write('当我们对视')\n",
    "    file_object.write('我们眼里有对方')\n",
    "    file_object.write('和色彩明丽的希望。')\n",
    "\n",
    "filename = 'programming.txt'\n",
    "\n",
    "with open(filename,'w') as file_object:\n",
    "    file_object.write('当我们对视\\n')\n",
    "    file_object.write('我们眼里有对方\\n')\n",
    "    file_object.write('和色彩明丽的希望\\n')\n",
    "\n",
    "\n",
    "## 10.2.3 附加到文件中\n",
    "\n",
    "filename = 'programming.txt'\n",
    "\n",
    "with open(filename,'a') as file_object:\n",
    "    file_object.write('眼底热烈的感情日渐无法隐藏\\n')\n",
    "    file_object.write('再这样下去我们只会双双狠狠跌伤\\n')\n",
    "    file_object.write('还没有能力承担任何可能发生的后果\\n')\n",
    "    file_object.write('于是一个眼神肯定，这份爱已有意义\\n')\n",
    "    file_object.write('我们举杯互助前程似锦\\n')\n",
    "    file_object.write('决然转身，立于对立的两端\\n')\n",
    "    file_object.write('再次相遇，眼底是燃烧的余烬，汹涌的暗流\\n')\n",
    "\n",
    "# 10.3 异常 try except 抵御无意的错误与恶意的攻击\n",
    "\n",
    "# 10.3.1 ZeroDivisionError\n",
    "\n",
    "print(5/0)\n",
    "\n",
    "# ## 10.3.2\n",
    "#\n",
    "try:\n",
    "    print(5/0)\n",
    "except ZeroDivisionError:\n",
    "    print(\"You can't divide by zero!\")\n",
    "\n",
    "## 10.3.3 创建一个执行除法运算的简单计算器\n",
    "\n",
    "print(\"Give me two numbers, and I'll divide them.\")\n",
    "print(\"Enter 'q' to quit.\")\n",
    "\n",
    "while True:\n",
    "    first_number = input(\"\\nFirst number:\")\n",
    "\n",
    "    if first_number == 'q':\n",
    "        break\n",
    "\n",
    "    second_number = input(\"\\nSecond number:\")\n",
    "    if second_number == 'q':\n",
    "        break\n",
    "\n",
    "    answer = int(first_number)/int(second_number)\n",
    "    print(answer)\n",
    "\n",
    "\n",
    "### 书上原话：P174，我仿佛得到了什么启示\n",
    "\n",
    "# 10.3.4\n",
    "\n",
    "print(\"Give me two numbers, and I'll divide them.\")\n",
    "print(\"Enter 'q' to quit.\")\n",
    "\n",
    "while True:\n",
    "    first_number = input(\"\\nFirst number:\")\n",
    "\n",
    "    if first_number == 'q':\n",
    "        break\n",
    "\n",
    "    second_number = input(\"\\nSecond number:\")\n",
    "    if second_number == 'q': ##这里感觉书上少些一句 P174\n",
    "        break\n",
    "\n",
    "    try:\n",
    "        answer = int(first_number)/int(second_number)\n",
    "\n",
    "    except ZeroDivisionError:\n",
    "        print(\"You can't divide by zero!\")\n",
    "\n",
    "    else:\n",
    "        print(answer)\n",
    "\n",
    "# 10.3.5 FileNotFoundError异常\n",
    "\n",
    "filename = 'alice.txt'\n",
    "\n",
    "with open(filename) as f_obj:\n",
    "    contents = f_obj.read\n",
    "\n",
    "filename = 'alice.txt'\n",
    "\n",
    "try:\n",
    "    with open(filename) as f_obj:\n",
    "        contents = f_obj.read\n",
    "# except FileExistsError:\n",
    "except FileNotFoundError:\n",
    "    msg=\"Sorry, the file \"+filename+\" does not exist.\"\n",
    "    print(msg)\n",
    "\n",
    "## 10.3.6 分析文本\n",
    "\n",
    "## 先测一下split()的功能：以空格为分隔符将字符串拆分\n",
    "title = 'shi jie da shen lun'\n",
    "a = title.split()\n",
    "print(a)\n",
    "\n",
    "title = '世界大神论\\n哈哈'\n",
    "a = title.split()\n",
    "print(a)\n",
    "b=len(a)\n",
    "print(b)\n",
    "\n",
    "## 查文件\n",
    "\n",
    "filename='programming.txt'\n",
    "\n",
    "try:\n",
    "    with open(filename) as f_obj:\n",
    "        contents = f_obj.read()\n",
    "except FileNotFoundError:\n",
    "    msg=\"\\nSorry, the file \"+filename+\" does not exist.\"\n",
    "    print(msg)\n",
    "\n",
    "else:\n",
    "    words = contents.split()\n",
    "    num_words = len(words)\n",
    "    print(words)\n",
    "    print(\"The file \"+ filename + \" has about \"+str(num_words)+\" words.\")\n",
    "\n",
    "    ### txt read后面的括号没打都会报错\n",
    "    ### 另外pycharm更新似乎很慢，文件改了，这边还没改\n",
    "\n",
    "# 10.3.7 将这个功能做成一个函数\n",
    "\n",
    "def count_words(filename):\n",
    "    \"\"\"计算一个文件大致包含多少单词\"\"\"\n",
    "    try:\n",
    "        with open(filename) as f_obj:\n",
    "            contents = f_obj.read()\n",
    "\n",
    "    except FileNotFoundError:\n",
    "        msg = \"\\nSorry, the file \" + filename + \" does not exist.\"\n",
    "        print(msg)\n",
    "\n",
    "    else:\n",
    "        words = contents.split()\n",
    "        num_words = len(words)\n",
    "        print(\"\\n\")\n",
    "        print(words)\n",
    "        print(\n",
    "            \"The file \" + filename + \" has about \" + str(num_words) + \" words.\")\n",
    "\n",
    "# filename='programming.txt'\n",
    "# count_words(filename)\n",
    "#\n",
    "\n",
    "filenames=['alice.txt','programming.txt','a_discussion_of_the_great_mind_of_our_world.txt']\n",
    "for filename in filenames:\n",
    "    count_words(filename)\n",
    "#\n",
    "# 10.3.8 失败时一声不吭\n",
    "\n",
    "def count_words(filename):\n",
    "    \"\"\"计算一个文件大致包含多少单词\"\"\"\n",
    "    try:\n",
    "        with open(filename) as f_obj:\n",
    "            contents = f_obj.read()\n",
    "\n",
    "    except FileNotFoundError:\n",
    "        pass\n",
    "\n",
    "    else:\n",
    "        words = contents.split()\n",
    "        num_words = len(words)\n",
    "        print(\"\\n\")\n",
    "        print(words)\n",
    "        print(\n",
    "            \"The file \" + filename + \" has about \" + str(num_words) + \" words.\")\n",
    "\n",
    "\n",
    "filenames=['alice.txt','programming.txt','a_discussion_of_the_great_mind_of_our_world.txt']\n",
    "for filename in filenames:\n",
    "    count_words(filename)\n",
    "\n",
    "# 10.4 存储数据\n",
    "#\n",
    "## 10.4.1\n",
    "\n",
    "import json\n",
    "\n",
    "numbers = [1,2,3,4,5,7,8]\n",
    "\n",
    "filename = 'numbers.json'\n",
    "with open(filename,'w') as f_obj:\n",
    "    json.dump(numbers,f_obj)\n",
    "\n",
    "filename = 'numbers.json'\n",
    "with open(filename) as f_obj:\n",
    "    n=json.load(f_obj)\n",
    "\n",
    "print(n)\n",
    "\n",
    "# 10.4.2 这个功能可以用来储存记录用户信息，比如记住我的名字和密码\n",
    "\n",
    "import json\n",
    "\n",
    "username = input(\"What is your name?\")\n",
    "\n",
    "filename = 'username.json'\n",
    "with open(filename,'w') as f_obj:\n",
    "    json.dump(username,f_obj)\n",
    "    print(\"We'll remember you when you come back, \"+username+\"!\")\n",
    "\n",
    "# 向储存了名字的用户发出问候\n",
    "\n",
    "import json\n",
    "\n",
    "filename = 'username.json'\n",
    "\n",
    "with open(filename) as f_obj:\n",
    "    username = json.load(f_obj)\n",
    "    print(\"Welcome back, \"+username+\"!\")\n",
    "\n",
    "## 两个程序合在一起\n",
    "\n",
    "import json\n",
    "# 如果以前存储了用户名就加载它\n",
    "# 否则，就提示用户输入用户名并存储它\n",
    "\n",
    "filename='username.json'\n",
    "try:\n",
    "    with open(filename) as f_obj:\n",
    "        username = json.load(f_obj) ## 尝试打开文件\n",
    "except FileNotFoundError:\n",
    "    username = input(\"What is your name?\") ##文件不存在，问你叫啥名\n",
    "    with open(filename,'w') as f_obj:\n",
    "        json.dump(username,f_obj)  ##输入的名字计入到目标文件中\n",
    "        print(\"We'll remember you when you come back, \"+username+\"!\")\n",
    "else:\n",
    "    print(\"Welcome back, \"+username+\"!\")\n",
    "\n",
    "# 10.4.3 重构 每个功能，都单独拿出来做成一个函数\n",
    "\n",
    "import json\n",
    "\n",
    "def greet_user(): ## 定义成一个函数\n",
    "    \"\"\"问候用户，并指出其名字\"\"\"\n",
    "\n",
    "    ### 上面的代码直接复制下来\n",
    "    filename = 'username.json'\n",
    "    try:\n",
    "        with open(filename) as f_obj:\n",
    "            username = json.load(f_obj)\n",
    "    except FileNotFoundError:\n",
    "        username = input(\"What is your name?\")\n",
    "        with open(filename, 'w') as f_obj:\n",
    "            json.dump(username, f_obj)\n",
    "            print(\"We'll remember you when you come back, \" + username + \"!\")\n",
    "    else:\n",
    "        print(\"Welcome back, \" + username + \"!\")\n",
    "\n",
    "greet_user()\n",
    "\n",
    "### 获取用户名，判断是否存储了用户名这部分分离出来\n",
    "\n",
    "import json\n",
    "\n",
    "def get_stored_username(filename):\n",
    "    \"\"\"如果存储了用户名，就获取它\"\"\"\n",
    "    filename = filename ### 我把这里改成函数的变量了\n",
    "    try:\n",
    "        with open(filename) as f_obj:\n",
    "            username=json.load(f_obj)\n",
    "    except FileNotFoundError:\n",
    "        return None ###这里用于下面做逻辑判断\n",
    "    else:\n",
    "        return username\n",
    "\n",
    "def greet_user(filename):\n",
    "    \"\"\"问候用户，并指出其名字\"\"\"\n",
    "    username=get_stored_username(filename)\n",
    "    if username:\n",
    "        print(\"Welcome back, \"+username+\"!\")\n",
    "    else:\n",
    "        username=input(\"What is your name?\")\n",
    "        filename = 'username.json'\n",
    "        with open(filename,'w') as f_obj:\n",
    "            json.dump(username,f_obj)\n",
    "            print(\"We'll remember you when you come back, \"+username+\"!\")\n",
    "\n",
    "greet_user('username.json')\n",
    "#\n",
    "## 将else下面这部分提出来，当成一个单独的函数\n",
    "\n",
    "import json\n",
    "\n",
    "def get_stored_username(filename):\n",
    "    \"\"\"如果存储了用户名，就获取它\"\"\"\n",
    "    filename = filename ### 我把这里改成函数的变量了\n",
    "    try:\n",
    "        with open(filename) as f_obj:\n",
    "            username=json.load(f_obj)\n",
    "    except FileNotFoundError:\n",
    "        return None ###这里用于下面做逻辑判断\n",
    "    else:\n",
    "        return username\n",
    "\n",
    "\n",
    "def get_new_username():\n",
    "    \"\"\"提示用户输入用户名\"\"\"\n",
    "    ### else下面的粘过来\n",
    "    username = input(\"What is your name?\")\n",
    "    filename = 'username.json'\n",
    "    with open(filename, 'w') as f_obj:\n",
    "        json.dump(username, f_obj)\n",
    "    return username\n",
    "\n",
    "\n",
    "def greet_user(filename):\n",
    "    \"\"\"问候用户，并指出其名字\"\"\"\n",
    "    username=get_stored_username(filename)\n",
    "    if username:\n",
    "        print(\"Welcome back, \"+username+\"!\")\n",
    "    else:\n",
    "        username=get_new_username()\n",
    "        print(\"We'll remember you when you come back, \"+username+\"!\")\n",
    "\n",
    "greet_user('user.json')\n",
    "\n",
    "# # 10.5 小结，结什么，第十章基础完结，开心，下面就是做题啦！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "## 10.3.7 将这个功能做成一个函数\n",
    "\n",
    "def count_words(filename):\n",
    "    \"\"\"计算一个文件大致包含多少单词\"\"\"\n",
    "    try:\n",
    "        with open(filename) as f_obj:\n",
    "            contents = f_obj.read()\n",
    "            # f_obj.flush()\n",
    "    except FileNotFoundError:\n",
    "        msg = \"\\nSorry, the file \" + filename + \" does not exist.\"\n",
    "        print(msg)\n",
    "\n",
    "    else:\n",
    "        words = contents.split()\n",
    "        num_words = len(words)\n",
    "        print(\"\\n\")\n",
    "        print(words)\n",
    "        print(\n",
    "            \"The file \" + filename + \" has about \" + str(num_words) + \" words.\")\n",
    "\n",
    "\n",
    "\n",
    "filenames=['alice.txt','programming.txt','a_discussion_of_the_great_mind_of_our_world.txt']\n",
    "for filename in filenames:\n",
    "    count_words(filename)\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "\n",
    "numbers = [1,2,3,4,5,7,8]\n",
    "\n",
    "filename = 'numbers.json'\n",
    "with open(filename,'w') as f_obj:\n",
    "    json.dump(numbers,f_obj)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 习题 10-1 文件打开的三种方式\n",
    "\n",
    "## 1\n",
    "print(\"1 文件.read()方式:\")\n",
    "with open('cp_pepa_1.txt') as file_object:\n",
    "    contents = file_object.read()\n",
    "    print(contents)\n",
    "\n",
    "## 2\n",
    "print(\"2 for line in 文件 方式:\")\n",
    "filename='cp_pepa_2.txt'\n",
    "\n",
    "with open(filename) as file_object:\n",
    "    for line in file_object:\n",
    "        print(line)\n",
    "\n",
    "# 3\n",
    "print(\"3 for line in 文件,加rstrip()处理:\")\n",
    "filename='cp_pepa_3.txt'\n",
    "with open(filename) as file_object:\n",
    "    for line in file_object:\n",
    "        print(line.rstrip())\n",
    "\n",
    "## 4\n",
    "print(\"4 文件.readlines() 方式:\")\n",
    "filename='cp_pepa_4.txt'\n",
    "\n",
    "with open(filename) as file_object:\n",
    "    lines = file_object.readlines()\n",
    "\n",
    "for line in lines:\n",
    "    print(line.rstrip())\n",
    "print(lines)\n",
    "\n",
    "# 10-2 replace() 字符串替换\n",
    "#\n",
    "msg=\"娘子 is real！\"\n",
    "print('\\n'+msg)\n",
    "msg=msg.replace('real','rio')\n",
    "print('\\n'+msg)\n",
    "\n",
    "\n",
    "print(\"替换前\")\n",
    "with open('cp_pepa_5.txt') as file_object:\n",
    "    contents = file_object.read()\n",
    "    print(contents)\n",
    "\n",
    "print(\"替换后\")\n",
    "with open('cp_pepa_5.txt') as file_object:\n",
    "    contents = file_object.read()\n",
    "    contents = contents.replace('顾依凉','他')\n",
    "    print(contents)\n",
    "\n",
    "## 对中文的替换是好使的，啊啊啊因为我打错了顾一凉。。。\n",
    "## 应为replace应该是以空格为单位在分割字符串，然后对比替换\n",
    "\n",
    "print(\"替换前\")\n",
    "with open('cp_pepa_6.txt') as file_object:\n",
    "    contents = file_object.read()\n",
    "    print(contents)\n",
    "\n",
    "print(\"替换后\")\n",
    "with open('cp_pepa_6.txt') as file_object:\n",
    "    contents = file_object.read()\n",
    "    contents = contents.replace('Yiliang GU','He')\n",
    "    print(contents)\n",
    "#\n",
    "# # ## 注意这个操作并没有改变文档本身\n",
    "#\n",
    "# # 10-3\n",
    "#\n",
    "filename = 'cp_pepa_7.txt'\n",
    "\n",
    "with open(filename,'w') as file_object:\n",
    "    msg=input(\"请输入:\")\n",
    "    file_object.write(msg)\n",
    "# #\n",
    "# # 你以为是新的大门向我敞开了吗？不，我探索见了一个全新的宇宙。我看见了一个美丽和谐欣欣向荣的正能量新世界。\n",
    "#\n",
    "# # 10-4 连续输入，一条占一行\n",
    "#\n",
    "filename = 'cp_pepa_8.txt'\n",
    "\n",
    "with open(filename,'w') as file_object:\n",
    "    while True:\n",
    "        msg=input(\"请输入:\")\n",
    "        if msg != 'q':\n",
    "            file_object.write(msg+'\\n')\n",
    "        else:\n",
    "            break\n",
    "\n",
    "# ##在这里，\n",
    "# # 我对顾依凉微笑，是眼含春风。\n",
    "# # 我跟顾依凉擦肩，是忍不回顾。\n",
    "# # 我与顾依凉同框时低下了头，是不胜娇羞。\n",
    "# # 我与顾依凉同框时转头与旁人说笑，是心仍相系。\n",
    "#\n",
    "# # 10-5 连续输入，一条占一行\n",
    "#\n",
    "# words=['在这里，',\n",
    "       '顾依凉对我微笑，是欲语还休。',\n",
    "       '顾依凉与我擦肩，是心中留恋。',\n",
    "       '顾依凉与我同框时低下了头，是强忍喜爱。',\n",
    "       '顾依凉与我同框时转头与旁人说笑，是假意避嫌。',\n",
    "       ]\n",
    "\n",
    "filename = 'cp_pepa_9.txt'\n",
    "\n",
    "with open(filename,'w') as file_object:\n",
    "    for word in words:\n",
    "        file_object.write(word+'\\n')\n",
    "#\n",
    "# 10-6 10-7 一起做了啊，想做一个计算器\n",
    "#\n",
    "print(\"Give me two numbers, and I'll add them together.\")\n",
    "print(\"Enter 'q' to quit.\")\n",
    "\n",
    "while True:\n",
    "    first_number = input(\"\\nFirst number:\")\n",
    "\n",
    "    if first_number == 'q':\n",
    "        break\n",
    "\n",
    "    second_number = input(\"\\nSecond number:\")\n",
    "    if second_number == 'q':\n",
    "        break\n",
    "\n",
    "    answer = int(first_number)+int(second_number)\n",
    "    print(answer)\n",
    "\n",
    "# # 10-6 10-7 excep ValueError 加法计算器\n",
    "\n",
    "print(\"Give me two numbers, and I'll add them together.\")\n",
    "print(\"Enter 'q' to quit.\")\n",
    "\n",
    "while True:\n",
    "    first_number = input(\"\\nFirst number:\")\n",
    "\n",
    "    if first_number == 'q':\n",
    "        break\n",
    "\n",
    "    second_number = input(\"\\nSecond number:\")\n",
    "    if second_number == 'q':\n",
    "        break\n",
    "\n",
    "    try:\n",
    "        answer = int(first_number)+int(second_number)\n",
    "    except ValueError:\n",
    "        print(\"不是数字你让我加什么加啊\")\n",
    "    else:\n",
    "        print(answer)\n",
    "\n",
    "# # 10-8 猫和狗 列表开文件，except 文件不存在\n",
    "#\n",
    "def read_txt(filenames):\n",
    "\n",
    "    for filename in filenames:\n",
    "        try:\n",
    "            with open(filename) as file_object:\n",
    "                contents = file_object.read()\n",
    "\n",
    "        except FileNotFoundError:\n",
    "            print('文件到底放哪了，整准了在让我找')\n",
    "\n",
    "        else:\n",
    "            print(contents)\n",
    "\n",
    "filenames=['cp_pepa_10.txt',\n",
    "           'cp_pepa_11.txt',\n",
    "           'cp_pepa_102.txt']\n",
    "\n",
    "read_txt(filenames)\n",
    "\n",
    "\n",
    "# # 10-9 猫和狗 列表开文件，except pass\n",
    "#\n",
    "def read_txt(filenames):\n",
    "\n",
    "    for filename in filenames:\n",
    "        try:\n",
    "            with open(filename) as file_object:\n",
    "                contents = file_object.read()\n",
    "\n",
    "        except FileNotFoundError:\n",
    "            pass\n",
    "\n",
    "        else:\n",
    "            print(contents)\n",
    "\n",
    "filenames=['cp_pepa_12.txt',\n",
    "           'cp_pepa_13.txt',\n",
    "           'cp_pepa_102.txt']\n",
    "\n",
    "read_txt(filenames)\n",
    "\n",
    "# # 10-10 count() 字符串中单词或短语出现了多少次\n",
    "#\n",
    "with open('cp_pepa_6.txt') as file_object:\n",
    "    contents = file_object.read()\n",
    "    print(contents)\n",
    "\n",
    "print(contents.count('Yiliang GU'))\n",
    "print(contents.count('yiliang gu'))\n",
    "print(contents.lower().count('yiliang gu'))\n",
    "\n",
    "# 10-11 10-12 json.dump 把书上的例子粘过来改\n",
    "\n",
    "import json\n",
    "\n",
    "def get_stored_number(filename):\n",
    "    \"\"\"如果存储了数字，就获取它\"\"\"\n",
    "    filename = filename ### 我把这里改成函数的变量了\n",
    "    try:\n",
    "        with open(filename) as f_obj:\n",
    "            number=json.load(f_obj)\n",
    "    except FileNotFoundError:\n",
    "        return None ###这里用于下面做逻辑判断\n",
    "    else:\n",
    "        return number\n",
    "\n",
    "\n",
    "def get_new_number():\n",
    "    \"\"\"提示用户输入用户名\"\"\"\n",
    "    ### else下面的粘过来\n",
    "    number = input(\"What is your favorite number?\")\n",
    "    filename = 'number.json'\n",
    "    with open(filename, 'w') as f_obj:\n",
    "        json.dump(number, f_obj)\n",
    "    return number\n",
    "\n",
    "\n",
    "def greet_user(filename):\n",
    "    \"\"\"问候用户，并指出其名字\"\"\"\n",
    "    number=get_stored_number(filename)\n",
    "    if number:\n",
    "        print(\"I know your favorite number! It's \"+number+\"!\")\n",
    "    else:\n",
    "        username=get_new_number()\n",
    "        print(\"We'll remember that when you come back!\")\n",
    "\n",
    "greet_user('number.json')\n",
    "# greet_user('.json') 可以再试一个不存在的文件\n",
    "\n",
    "# # 10-13 没看懂\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
